package cn.edu.kzjsxy.graduationproject.controller;


import cn.edu.kzjsxy.graduationproject.common.Page;
import cn.edu.kzjsxy.graduationproject.common.Result;
import cn.edu.kzjsxy.graduationproject.dto.StudentDormitoryAssignment;
import cn.edu.kzjsxy.graduationproject.entity.Buildings;
import cn.edu.kzjsxy.graduationproject.entity.StudentDormAssignments;
import cn.edu.kzjsxy.graduationproject.entity.Users;
import cn.edu.kzjsxy.graduationproject.service.IBuildingsService;
import cn.edu.kzjsxy.graduationproject.service.IStudentDormAssignmentsService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 学生宿舍分配表 前端控制器
 * </p>
 *
 * @author chén jiā jùn
 * @since 2024-04-14
 */
@Api(tags = "学生宿舍分配表")
@RestController
@RequestMapping("/studentDormAssignments")
public class StudentDormAssignmentsController {

//	学生宿舍分配的service
	@Autowired
	private IStudentDormAssignmentsService studentDormAssignmentsService;

//	楼栋信息表的service
	@Autowired
	private IBuildingsService buildingsService;


	@GetMapping("/{assignmentId}")
	@ApiOperation("根据学生分配宿舍ID查询学生分配宿舍信息")
	public Result<StudentDormAssignments> getStudentDormAssignmentsById(@PathVariable String assignmentId) {
		StudentDormAssignments studentDormAssignments = studentDormAssignmentsService.getById(assignmentId);
		return Result.success(studentDormAssignments);
	}

	@ApiOperation("添加学生宿舍分配信息")
	@PostMapping("/add")
	public Result addStudentDormAssignment(@RequestBody StudentDormAssignments studentDormAssignment) {
		boolean success = studentDormAssignmentsService.save(studentDormAssignment);
		return Result.judgment(success);
	}

	@ApiOperation("删除学生宿舍分配信息")
	@DeleteMapping("/delete/{assignmentId}")
	public Result deleteStudentDormAssignment(@PathVariable String assignmentId) {
		boolean success = studentDormAssignmentsService.removeById(assignmentId);
		return Result.judgment(success);
	}

	@ApiOperation("更新学生宿舍分配信息")
	@PutMapping("/update")
	public Result updateStudentDormAssignment(@RequestBody StudentDormAssignments studentDormAssignment) {
		boolean success = studentDormAssignmentsService.updateById(studentDormAssignment);
		return Result.judgment(success);
	}

	@ApiOperation("根据条件查询学生宿舍分配信息")
	@PostMapping("/list")
	public Result<List<StudentDormAssignments>> selectStudentDormAssignmentsByCondition(@RequestBody StudentDormAssignments studentDormAssignment) {
		List<StudentDormAssignments> assignmentsList = studentDormAssignmentsService.selectStudentDormAssignmentsByCondition(studentDormAssignment);
		return Result.success(assignmentsList);
	}

	@ApiOperation("分页获取学生宿舍分配信息")
	@PostMapping("/page")
	public Result<PageInfo<StudentDormAssignments>> selectStudentDormAssignmentsByPage(Page page, @RequestBody StudentDormAssignments studentDormAssignment) {
		return studentDormAssignmentsService.selectStudentDormAssignmentsByPage(page, studentDormAssignment);
	}

	@ApiOperation("查询学生宿舍分配信息 (多表查询)")
	@PostMapping("/getStudentDormAssignments")
	public Result<List<StudentDormitoryAssignment>> getStudentDormAssignments(@RequestBody StudentDormitoryAssignment studentDormitoryAssignment) {
		List<StudentDormitoryAssignment> studentDormAssignments = studentDormAssignmentsService.getStudentDormAssignments(studentDormitoryAssignment);
		return Result.success(studentDormAssignments);
	}

	@ApiOperation("分页查询学生宿舍分配信息 (多表查询)")
	@PostMapping("/getStudentDormAssignmentsPage")
	public
	Result<PageInfo<StudentDormitoryAssignment>> getStudentDormAssignmentsPage(Page page, @RequestBody StudentDormitoryAssignment studentDormitoryAssignment, HttpServletRequest request) {
		return studentDormAssignmentsService.getStudentDormAssignmentsPage(page,studentDormitoryAssignment,request);
	}
	@ApiOperation("查询未分配的学生信息（多表查询）")
	@GetMapping("/getStudentsWithoutDormitory/{buildingId}")
	public Result<List<Users>> getStudentsWithoutDormitory(@PathVariable  String buildingId) {
		List<Users> studentsWithoutDormitory = studentDormAssignmentsService.getStudentsWithoutDormitory();
		List<Users> schoolboy =new ArrayList<>(); //男学生
		List<Users> schoolgirl=new ArrayList<>();//女学生
		Buildings buildings =null;
		if(buildingId!=null){
			buildings=buildingsService.getById(buildingId);
		}
		if(buildings==null){
			return Result.error("楼栋信息不存在！");
		}
		Integer dormitoryType = buildings.getDormitoryType();// 0是女 1是男 2是混合
		if(studentsWithoutDormitory!=null&&!studentsWithoutDormitory.isEmpty()){
			for (Users users : studentsWithoutDormitory) {
				if (dormitoryType == 0) {
					schoolgirl.add(users);
				} else if (dormitoryType == 1) {
					schoolboy.add(users);
				}
			}
		}
		if (dormitoryType == 0) {
			return Result.success(schoolgirl);
		} else if (dormitoryType == 1) {
			return Result.success(schoolboy);
		}
		return Result.success(studentsWithoutDormitory);
	}





}
